package com.ljb.jsoup.ziroom;

import com.alibaba.fastjson.JSONObject;
import com.ljb.jsoup.ziroom.ocr.BaiDuOCR;
import com.ljb.jsoup.ziroom.ocr.WordsResult;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 自如房屋信息抓取
 */
public class ZiroomFetchMain {
    public static final String URL = "http://www.ziroom.com/z/nl/z3.html?p=";
    //信息
    public static List<String> infos = new ArrayList<String>();
    //价格
    public static List<String> prices = new ArrayList<String>();
    public static int count;


    public static void main(String[] args) throws IOException {
//        testOCRFromUrl();
        testOCRFromImage();
//        testZiroomInfo();
    }

    /**
     * 本地图片ocr识别测试
     *
     * @throws IOException
     */
    private static void testOCRFromImage() throws IOException {
        for (int i = 0; i < 5000; i++) {
            try {
                List<WordsResult.WordsResultBean> ocr = BaiDuOCR.getDefault().ocrImage("E:/IdeaProjects/JsoupDemo/ceshi.jpeg");
                System.out.println("index= " + count++ + " ocr=" + ocr);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 网络图片ocr测试
     */
    private static void testOCRFromUrl() {
        for (int i = 0; i < 5000; i++) {
            try {
                List<WordsResult.WordsResultBean> ocr = BaiDuOCR.getDefault().ocrImageUrl("http://aip.bdstatic.com/portal/dist/1557827079882/ai_images/technology/ocr/general/demo/accurate_basic/1.jpg");
                System.out.println("index= " + count++ + " ocr=" + ocr);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 自如房价识别
     *
     * @throws IOException
     */
    private static void testZiroomInfo() throws IOException {
        //获取页码信息
        int pageNumber = getPageInfo();
        //抓取数据
        for (int i = 1; i <= pageNumber; i++) {
            getZiroom(i);
        }

        System.out.println("================================房屋信息及报价=================================");
        for (int i = 0; i < infos.size(); i++) {
            System.out.println("记录:" + i + "  " + infos.get(i) + " " + prices.get(i));
        }
    }

    /**
     * 获取页码信息
     *
     * @return
     * @throws IOException
     */
    private static int getPageInfo() throws IOException {
        Document doc = Jsoup.connect(URL).get();
        //获取页码信息
        Element page = doc.getElementById("page");
        int pageNumber = 1;
        if (page.childNodeSize() > 1) {
            String page_string = page.text();
            int start = page_string.indexOf("共");
            int end = page_string.indexOf("页", start);
            String substring = page_string.substring(start + 1, end);
            pageNumber = Integer.parseInt(substring);
        }
        System.out.println("页数:" + pageNumber);
        return pageNumber;
    }

    /**
     * 根据页码抓取数据
     *
     * @param pageNumber
     * @throws IOException
     */
    private static void getZiroom(int pageNumber) throws IOException {
        Document doc = Jsoup.connect(URL + pageNumber).get();

        //解析房屋信息
        Element houseList = doc.getElementById("houseList");
        Elements clearfixs = houseList.getElementsByTag("li");
        for (Element clearfix : clearfixs) {
            Elements txtList = clearfix.getElementsByClass("txt");
            Elements unit = clearfix.getElementsByClass("priceDetail").get(0).getElementsByClass("gray-6");
            infos.add(txtList.text() + " " + unit.text());
        }

        //解析房屋价格
        String script = doc.getElementsByTag("script").get(5).data();
        int start = script.indexOf("{\"image\"");
        int end = script.indexOf(";", start);
        String substring = script.substring(start, end);
        PriceImageBean imageBean = JSONObject.parseObject(substring, PriceImageBean.class);

        //识别房屋价格
        List<List<Integer>> offsets = imageBean.offset;
        List<WordsResult.WordsResultBean> ret = BaiDuOCR.getDefault().ocrImageUrl("http:" + imageBean.image);
        if (ret != null) {
            String price_string = ret.get(0).words;
            for (List<Integer> offset : offsets) {
                StringBuilder price = new StringBuilder();
                for (Integer anOffset : offset) {
                    price.append(price_string.charAt(anOffset));
                }
                prices.add(price.toString());
            }
        }
    }
}
